﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using sixgod.Common.Model;
using sixgod.Dtos.System;
using sixgod.Dtos.User;
using sixgod.Dtos.WorkFlow;
using sixgod.Enum;
using sixgod.Model.System;
using sixgod.Model.System.Menu;
using sixgod.Model.User;
using sixgod.Model.WorkFlow;
using sixgod.Model.WorkFlowTemplate;
using SqlSugar;

namespace Common.AutoMapper
{
    public class AutoProfile:Profile
    {
        public AutoProfile()
        {
            SystemMap();
            UserMap();
            WorkFlowMap();
        }

        public void SystemMap()
        {
            CreateMap<SystemMenu,MenuDto>()
                .ForMember(dto=>dto.MenuName,opt=>opt.MapFrom(src=>src.Name))
                .ForMember(dto=>dto.Code,opt=>opt.MapFrom(src=>src.Id))
                .ForMember(dto=>dto.ParentMenu,opt=>opt.MapFrom(src=>src.ParentCode));

            CreateMap<SystemDictionary,DictionaryDto>();
            CreateMap<DictionaryItem,DicItemDto>()
                .ForMember(dto=>dto.DicCode,opt=>opt.MapFrom(src=>src.ParentCode));

            CreateMap<PageQueryModel,PageModel>()
                .ForMember(dto=>dto.PageSize,opt=>opt.MapFrom(src=>src.PageSize))
                .ForMember(dto=>dto.PageIndex,opt=>opt.MapFrom(src=>src.PageNum));

            CreateMap<SystemDictionary,SelectDto>()
                .ForMember(dto=>dto.Value,opt=>opt.MapFrom(src=>src.Code));
        }

        public void UserMap()
        {
            CreateMap<User,UserSelectDto>();
            CreateMap<Job,JobDto>();
            CreateMap<Department,DepartmentDto>();

            CreateMap<User,UserDto>()
                .ForMember(dto=>dto.Department,opt=>opt.MapFrom(src=>src.Department.Name))
                .ForMember(dto=>dto.Job,opt=>opt.MapFrom(src=>src.Job.Name));
        }

        public void WorkFlowMap()
        {
            CreateMap<WorkFlowTemplateInputDto,WorkFlowTemplate>();
            CreateMap<WorkFlowTemplate,WorkFlowTemplateOutDto>();

            CreateMap<TemplateFormItemDto,TemplateFormItem>();
            CreateMap<TemplateFormItem,TemplateFormItemDto>();

            CreateMap<WorkFlowTemplateNode,WorkFlowTemplateNodeDto>()
                .ForMember(dto=>dto.type,opt=>opt.MapFrom(src=> (int)src.EnumNodeType ))
                .ForMember(dto=>dto.auditMode,opt=>opt.MapFrom(src=> (int)src.EnumAuditMode ));
            CreateMap<WorkFlowTemplateNodeDto,WorkFlowTemplateNode>()
                .ForMember(r=>r.EnumNodeType,opt=>opt.MapFrom(src=> (EnumNodeType) Convert.ToInt32(src.type)))
                .ForMember(r=>r.EnumAuditMode,opt=>opt.MapFrom(src=> (EnumAuditMode)Convert.ToInt32(src.auditMode)));

            CreateMap<TemplateNodeLink,TemplateNodeLinkDto>();
            CreateMap<TemplateNodeLinkDto,TemplateNodeLink>();

            CreateMap<TemplateNodeLink,NodeLink>();

            CreateMap<WorkFlowTemplateNode,WorkFlowNode>();

            CreateMap<WorkFlow,WorkFlowDto>()
                .ForMember(dto=>dto.CurrentNode,opt=>opt.MapFrom(src=>src.CurrentNode.Name))
                .ForMember(dto=>dto.Initiator,opt=>opt.MapFrom(src=>src.InitUser.Name))
                .ForMember(dto=>dto.Level,opt=>opt.MapFrom(src=>(int)src.Level))
                .ForMember(dto=>dto.RebackUserName,opt=>opt.MapFrom(src=>src.RebackUser.Name));

            CreateMap<FormItem,FormItemDto>();

            CreateMap<AuditPerson,AuditInfo>()
                .ForMember(dto=>dto.AuditTime,opt=>opt.MapFrom(
                    src=> src.Result == EnumNodeOperation.Start || src.Result == EnumNodeOperation.End?null:src.AuditTime))
                .ForMember(dto=>dto.UserName,opt=>opt.MapFrom(src=>src.Person.Name))
                .ForMember(dto=>dto.Result,opt=>opt.MapFrom(src=>src.Result == null ? null: ((int)src.Result).ToString()));
        }
    }
}
